package com.triptogether.shoppinglist.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.triptogether.R;
import com.triptogether.utils.db.DBManager;
import com.triptogether.utils.db.models.ShoppingList;
import com.triptogether.utils.log.Logger;

public class ShoppingListsListAdapter extends BaseAdapter {

	private final String TAG = Logger.makeLogTag(this.getClass());

	private List<ShoppingList> shoppinglistList;
	private LayoutInflater mInflater;
	private OnItemSelectedListener onItemSelectedListener;

	public ShoppingListsListAdapter(Context context, List<ShoppingList> shoppingLists) {
		this.shoppinglistList = (ArrayList<ShoppingList>) shoppingLists;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public ShoppingListsListAdapter(Context context, Long tripId) {
		this(context, (ArrayList<ShoppingList>) DBManager.getInstance(context).findShoppingListsByTripId(tripId));
	}

	/**
	 * Generate the view that represents the list. Choose the layout that should
	 * be displayed according to the {@link ShoppingList}
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// Check if the convert view is null, and inflate it when necessary
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_shopping_list, null);
		}
		
		// Add the clickListener for this view if exsists
		if(this.onItemSelectedListener != null) {
			convertView.findViewById(R.id.textview_shopping_list_name).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onItemSelectedListener.onShoppingListSelected(position);
				}
			});
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onItemSelectedListener.onShoppingListSelected(position);
				}
			});
		}
		
		try {
			ShoppingList shoppinglistToInit = shoppinglistList.get(position);

			// Initialize the view UI
			initItemViewUI(convertView, shoppinglistToInit);

		} catch (Exception e) {
			Logger.LOGE(TAG, e.getMessage());
		}

		return convertView;
	}

	private void initItemViewUI(View convertView, ShoppingList shoppinglistToInit) {
		TextView shoppingListNameTextView = (TextView) convertView.findViewById(R.id.textview_shopping_list_name);
		shoppingListNameTextView.setText(shoppinglistToInit.getShoppingListName());
    }

	public void add(ShoppingList newShoppingList) {
		shoppinglistList.add(newShoppingList);
		notifyDataSetChanged();
	}

	@Override
    public int getCount() {
		return shoppinglistList.size();
    }

	@Override
    public Object getItem(int position) {
		return shoppinglistList.get(position);
    }

	@Override
	public long getItemId(int position) {
		return shoppinglistList.get(position).getId();
	}
	
	// //////////////////////////////////////////////////
	// Listener
	// //////////////////////////////////////////////////
	
	public interface OnItemSelectedListener {
		public void onShoppingListSelected(int position);
	}
	
	public void setOnItemSelectedListener(OnItemSelectedListener listener) {
		this.onItemSelectedListener = listener;
	}
	
	// //////////////////////////////////////////////////
	// Listener
	// //////////////////////////////////////////////////
}
